package com.xzy.urp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.xzy.urp.common.exception.Assert;
import com.xzy.urp.common.result.ResponseEnum;
import com.xzy.urp.listener.UserExcelListener;
import com.xzy.urp.pojo.dto.ExcelUserDTO;
import com.xzy.urp.pojo.entity.UserInfo;
import com.xzy.urp.mapper.UserInfoMapper;
import com.xzy.urp.pojo.vo.UserInfoVO;
import com.xzy.urp.pojo.vo.UserVO;
import com.xzy.urp.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzy.urp.utils.JwtUtils;
import com.xzy.urp.utils.MD5Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzy
 * @since 2022-02-17
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Override
    public void register(UserVO registerVO) {

        //数据均可重复，唯一不可重复的为主键，所以没有数据重复判断
        UserInfo userInfo = new UserInfo();
        BeanUtil.copyProperties(registerVO,userInfo);
        String salt = UUID.randomUUID().toString().substring(0, 10);
        userInfo.setUSalt(salt);
        userInfo.setUPassword(MD5Utils.encrypt(registerVO.getUPassword(),salt));

        //插入操作


        baseMapper.insert(userInfo);





    }


    @Override
    public UserInfoVO login(UserVO loginVO) {

        //主键获取用户
        UserInfo userInfo = baseMapper.selectById(loginVO.getUId());


        //判断用户是否存在
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);

        //校验密码

        Assert.equals(userInfo.getUPassword(),
                MD5Utils.encrypt(loginVO.getUPassword(),userInfo.getUSalt()),ResponseEnum.LOGIN_PASSWORD_ERROR);
        Assert.isTrue(loginVO.getUType()==userInfo.getUType(),ResponseEnum.LOGIN_MOBILE_ERROR);

        //校验通过  对象复制
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtil.copyProperties(userInfo,userInfoVO);


        //生成token
        String token = JwtUtils.createToken(userInfo.getUId(), userInfo.getUType());
        userInfoVO.setToken(token);

        return userInfoVO;

    }


    @Override
    public void updatePwd(Long uid, String old, String news) {
        UserInfo userInfo = baseMapper.selectById(uid);

        //判断用户是否存在
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);

        //校验密码
        Assert.equals(userInfo.getUPassword(),
                MD5Utils.encrypt(old,userInfo.getUSalt()),ResponseEnum.LOGIN_PASSWORD_ERROR);

        //修改密码
        userInfo.setUPassword(MD5Utils.encrypt(news,userInfo.getUSalt()));

        baseMapper.updateById(userInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importData(InputStream inputStream) {

        EasyExcel.read(inputStream, ExcelUserDTO.class,new UserExcelListener(baseMapper)).sheet().doRead();



    }

    @Override
    public void updateInfo(UserInfoVO userInfoVO) {
        UserInfo userInfo = baseMapper.selectById(userInfoVO.getUId());
        userInfo.setUEmail(userInfoVO.getUEmail());
        userInfo.setUPhone(userInfoVO.getUEmail());
        baseMapper.updateById(userInfo);
    }


}
